﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Media3D;

namespace LxCFD
{
    public partial class ZoneCFDFields
    {
        public EquationBase T;
        public void CheckCreate_T()
        {
            if (PhysicalModel.IsEnergyEnabled)
            {
                if (PhysicalModel.PhysicalType == PhysicalTypes.Solid)
                {
                    if (T == null || !(T is PossionEquation))
                    {
                        var newT = new PossionEquation();
                        newT.CopyFromOldEquation(T);
                        T = newT;
                        T.CheckCreate(Zone);
                    }
                }
                else
                {
                    if (T == null || !(T is NSEquation))
                    {
                        var newT = new NSEquation();
                        newT.CopyFromOldEquation(T);
                        T = newT;
                        T.CheckCreate(Zone);
                    }
                }
                AppRes.SolveCenter.TemperatureGroup.Equations.Add(T);
                AppRes.SolveCenter.InitializeGroup.Initialization_Normal6.Add(T);
                AppRes.MonitorCenter.Temperature.Add(T);
            }
            else
            {
                T = null;
            }
        }
        public void SetParameter_T()
        {
            if (T == null) return;
            if (T is PossionEquation te)
            {
                te.SetZone_CommonTerm(Material.ThermalConductivity.GetCopy(Zone), Zone.EnergySource.GetACopy(Zone), PhysicalModel.InitialTemperature.GetCopy(Zone), 1);
                if (AppRes.SolveCenter.IsTransient)
                {
                    T.SetZone_TransTerm(true, Material.SpecificHeat.GetCopy(Zone), Density);
                    AppRes.SolveCenter.InitializeGroup.Initialization_Transient9.Add(T.TransData);
                }
                else
                {
                    T.SetZone_TransTerm(false, null, null);
                }
                foreach (var t in Zone.BoundThreads)
                {
                    if (t.IsEnableCoupled)
                    {
                        te.SetBound_Coupled(t);
                        continue;
                    }
                    switch (t.WallThermalType)
                    {
                        case WallThermalTypes.FixTemperature:
                            te.SetBoundary_FixValue(t.Temperature.GetCopy(Zone), t);
                            break;
                        case WallThermalTypes.FixHeatFlux:

                            te.SetBoundary_FixFlux(t.HeatFlux.GetCopy(Zone), t);
                            break;
                        case WallThermalTypes.FixhT:
                            te.SetBoundary_FixCoupledhT(t.Coupledh.GetCopy(Zone), t.CoupledT.GetCopy(Zone), t);
                            break;
                    }
                }
            }
            else
            {
                var tn = T as NSEquation;
                Value1Base<double> tDiff = GammaCommon == null ? Material.ThermalConductivity.GetCopy(Zone) : GammaCommon;
                Value1Base<double> specificHeat = Material.SpecificHeat.GetCopy(Zone);
                tn.SetZone_CommonTerm(tDiff, Zone.EnergySource.GetACopy(Zone), PhysicalModel.InitialTemperature.GetCopy(Zone), 0.9);
                tn.SetZone_ConvTerm(specificHeat, PhysicalModel.ConvectionScheme, Velocity);
                if (AppRes.SolveCenter.IsTransient)
                {
                    tn.SetZone_TransTerm(true, specificHeat, Density);
                    AppRes.SolveCenter.InitializeGroup.Initialization_Transient9.Add(T.TransData);
                }
                else
                {
                    tn.SetZone_TransTerm(false, null, null);
                }

                foreach (var thread in Zone.BoundThreads)
                {
                    if (thread.IsEnableCoupled)
                    {
                        tn.SetBoundary_Coupled_Wall(thread);
                        continue;
                    }
                    switch (thread.BoundaryType)
                    {
                        case BoundaryTypes.FluidWall:
                            switch (thread.WallThermalType)
                            {
                                case WallThermalTypes.FixTemperature:
                                    tn.SetBoundary_fixValue(thread.Temperature.GetCopy(Zone), thread);
                                    break;
                                case WallThermalTypes.FixHeatFlux:
                                    if (thread.HeatFlux.IsEqual0())
                                        tn.SetBoundary_symmetry(thread);
                                    else
                                        throw new Exception("液体壁面仅支持定温度或绝热");
                                    break;
                                default:
                                    throw new Exception("液体壁面仅支持定温度或绝热");
    
                            }
                            break;
                        case BoundaryTypes.Symmetry:
                            tn.SetBoundary_symmetry(thread);
                            break;
                        case BoundaryTypes.VelocityInlet:
                        case BoundaryTypes.PressureInOut:
                            tn.SetBoundary_flow(thread.Temperature, thread);
                            break;
                        default:
                            LxConsole.Error("未知边界类型");
                            break;
                    }
                }

            }
        }

    }
}
//Value1Const.Double Tau = new Value1Const.Double(materialSolid.thermalConductivity);
//Value1Base TauLocal = Tau.copy();
//Value1Const.Double iniTem = new Value1Const.Double(zone.continuum.iniTemperature);
//var threads = zone.BoundThreads;
//for (int i1 = 0; i1 < threads.Count; i1++)
//{
//    if (threads[i1].isEnableCoupled)
//    {
//        T.setBound_coupled(i1, threads[i1]);
//        continue;
//    }
//    switch (threads[i1].wallThermalType)
//    {
//        case WallThermalTypes.定壁温:
//            T.setBoundary_fixValue(threads[i1].T, i1, threads[i1]);
//            break;
//        case WallThermalTypes.定热流密度:
//            T.setBoundary_fixFlux(threads[i1].heatFlux, i1, threads[i1]);
//            break;
//        case WallThermalTypes.定系数:
//            T.setBoundary_fixCoupledhT(threads[i1].coupledh, threads[i1].coupledT, i1, threads[i1]);
//            break;
//        default:
//            break;
//    }
//}
//T.SetMultiGridAAtLast(zone.AMGSolver.MGLocal_A);
//public override bool checkMatrixs()
//{
//    if (T != null)
//    {
//        string checkmes = T.checkMatrix();
//        if (checkmes != "")
//        {
//            LxConsole.AddLine(LogTypes.错误, checkmes + " - " + zone.name);
//            return false;
//        }
//    }

//    return true;
//}
//public override void initialize_ifNeeded(int taskIndex)
//{
//    if (T == null) return;
//    T.initialize_ifNeeded(taskIndex);
//}
//public override void initialize_force(int taskIndex)
//{
//    if (T == null) return;
//    T.initialize_force(taskIndex);
//}
//public override void updateMatrix(int taskIndex)
//{
//    if (T != null)
//    {
//        T.set_x_Sync(taskIndex);
//        T.calInnerGradPhi_Sync(taskIndex);
//        T.set_b_Sync(taskIndex);
//        T.set_A_LU_Sync(taskIndex);
//        T.set_A_Diag_Sync(taskIndex);
//        T.calFaceNonOrthogonal_Sync(taskIndex);
//        T.correctNonOrthogonal_Sync(taskIndex);
//        T.relax_Sync(taskIndex);
//    }
//}
//public override void solve(int taskIndex)
//{
//    if (T != null)
//    {
//        zone.MultiGridSolver.Solve_VCycle(T, taskIndex);
//        T.cal_x_Jacobi_Sync(taskIndex);
//        //T.cal_x_GS_Sync(taskIndex);

//        T.updateBound_Sync(taskIndex);

//        TasksControl.syncTasksWithOneAction(taskIndex, printResult);
//    }
//}
//private void printResult()
//{
//    foreach (Mesh.Thread bt in zone.BoundThreads)
//    {
//        double ave = 0;
//        for (int i1 = bt.ThreadIndex.StartCellIndex; i1 <= bt.ThreadIndex.EndCellIndex; i1++)
//        {
//            ave += T.x[i1];
//        }
//        Console.WriteLine("{0}:net--{1}", bt.name, ave / (bt.ThreadIndex.EndCellIndex - bt.ThreadIndex.StartCellIndex + 1));
//    }
//}
//松弛处理
//double relaxFactor = 1;
//if (relaxFactor <=0)
//{
//    MainViewModel.mainViewModel.log.AddWithTime("松弛因子应大于0");
//}else if(relaxFactor != 1)//有松弛的时候
//{
//    for (int i1 = 0; i1 < temperatureMatrix.A_Diag.Length; i1++)
//    {
//        temperatureMatrix.A_Diag[i1] /= relaxFactor;                      
//    }
//    Solver += () =>
//    {
//        for (int i3 = 0; i3 < temperatureMatrix.b.Length; i3++)
//        {
//            temperatureMatrix.b[i3] += (1-relaxFactor)*temperatureMatrix.A_Diag[i3]*temperatureMatrix.phi[i3];
//        }
//    };
//}